package Easy两数之和;

import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.Map;

public class Solution {
    public int[] twoSum1(int[] nums, int target) {
        Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
        for (int i = 0; i < nums.length; ++i) {
            if (hashtable.containsKey(target - nums[i])) {
                return new int[]{hashtable.get(target - nums[i]), i};
            }
            hashtable.put(nums[i], i);
        }
        return new int[0];


//        for(int i=0; i<nums.length-1; i++)
//            for (int j = i; j < nums.length-1; j++) {
//                if (target == nums[i]+nums[j+1]){
//                    num[0] = i;
//                    num[1] = j+1;
//                    return num;
//                }
//            }
    }

    public int[] twoSum(int[] nums, int target) {
        int[] num = new int[2];
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.getOrDefault(target-nums[i],-1) != -1){
                num[0] = map.getOrDefault(target-nums[i],-1);
                num[1] = i;
                break;
            }
            map.put(nums[i], i);
        }
        return num;
    }
    @Test
    public void test(){
        int[] nums = {-3,4,3,9};
        int[] num;
        num = twoSum(nums,0);
        for (int i:num) {
            System.out.println(i);
        }
    }
}
